import com.google.common.hash.Hashing;
import java.nio.charset.StandardCharsets;

/**
 * 简单的哈希测试工具 - 独立运行
 */
public class SimpleHashTest {

    /**
     * 自己实现的 MurmurHash3（来自 UsersServiceImpl）
     */
    private static int murmurHash3Custom(String key) {
        byte[] data = key.getBytes();
        int length = data.length;
        int seed = 0;
        
        int c1 = 0xcc9e2d51;
        int c2 = 0x1b873593;
        int r1 = 15;
        int r2 = 13;
        int m = 5;
        int n = 0xe6546b64;
        
        int hash = seed;
        int nblocks = length / 4;
        
        for (int i = 0; i < nblocks; i++) {
            int k = getblock(data, i * 4);
            k *= c1;
            k = rotl32(k, r1);
            k *= c2;
            
            hash ^= k;
            hash = rotl32(hash, r2);
            hash = hash * m + n;
        }
        
        int tail = nblocks * 4;
        int k1 = 0;
        switch (length & 3) {
            case 3:
                k1 ^= (data[tail + 2] & 0xff) << 16;
            case 2:
                k1 ^= (data[tail + 1] & 0xff) << 8;
            case 1:
                k1 ^= (data[tail] & 0xff);
                k1 *= c1;
                k1 = rotl32(k1, r1);
                k1 *= c2;
                hash ^= k1;
        }
        
        hash ^= length;
        hash = fmix(hash);
        
        return hash;
    }
    
    private static int getblock(byte[] data, int i) {
        return ((data[i] & 0xff) | 
                ((data[i + 1] & 0xff) << 8) | 
                ((data[i + 2] & 0xff) << 16) | 
                ((data[i + 3] & 0xff) << 24));
    }
    
    private static int rotl32(int x, int r) {
        return (x << r) | (x >>> (32 - r));
    }
    
    private static int fmix(int h) {
        h ^= h >>> 16;
        h *= 0x85ebca6b;
        h ^= h >>> 13;
        h *= 0xc2b2ae35;
        h ^= h >>> 16;
        return h;
    }

    /**
     * Guava 的 MurmurHash3
     */
    private static int murmurHash3Guava(String key) {
        return Hashing.murmur3_32_fixed()
                .hashString(key, StandardCharsets.UTF_8)
                .asInt();
    }

    public static void main(String[] args) {
        String[] testPhones = {
            "13600005769",  // Python 结果: Hash=-1447825385, data_85
            "13600008583"   // Python 结果: Hash=408026922, data_122
        };

        System.out.println("=".repeat(80));
        System.out.println("MurmurHash3 实现对比测试");
        System.out.println("=".repeat(80));
        System.out.println();

        for (String phone : testPhones) {
            int customHash = murmurHash3Custom(phone);
            int guavaHash = murmurHash3Guava(phone);
            
            int customTable = Math.abs(customHash) % 200;
            int guavaTable = Math.abs(guavaHash) % 200;

            System.out.println("电话号码: " + phone);
            System.out.println("  自定义实现 (核验功能使用):");
            System.out.println("    Hash值: " + customHash);
            System.out.println("    表索引: " + customTable);
            System.out.println("    目标表: data_" + customTable);
            System.out.println("  Guava实现 (ShardingSphere使用):");
            System.out.println("    Hash值: " + guavaHash);
            System.out.println("    表索引: " + guavaTable);
            System.out.println("    目标表: data_" + guavaTable);
            
            boolean same = (customHash == guavaHash);
            System.out.println("  是否一致: " + (same ? "✅ 是" : "❌ 否 【问题！】"));
            System.out.println();
        }

        System.out.println("=".repeat(80));
        System.out.println("Python mmh3.hash() 预期结果:");
        System.out.println("  13600005769 -> Hash: -1447825385 -> 表索引: 85 -> data_85");
        System.out.println("  13600008583 -> Hash: 408026922 -> 表索引: 122 -> data_122");
        System.out.println("=".repeat(80));
    }
}

